package com.java.mall_imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.mall_imooc.common.utils.PageUtils;
import com.java.mall_imooc.common.utils.R;
import com.java.mall_imooc.entity.Shipping;
import com.java.mall_imooc.form.ShippingForm;
import com.java.mall_imooc.mapper.ShippingDao;
import com.java.mall_imooc.service.ShippingService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("shippingService")
public class ShippingServiceImpl extends ServiceImpl<ShippingDao, Shipping> implements ShippingService {

    @Override
    public PageUtils queryPage(Shipping shipping,Integer pageNum, Integer pageSize) {
        IPage<Shipping> page = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<Shipping>().eq(Shipping::getUserId,shipping.getUserId()) // 拼接查询条件
        );

        return new PageUtils(page);
    }

    @Override
    public R<Map<String,Integer>> add(Integer userId, ShippingForm shippingForm) {
        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingForm,shipping);
        shipping.setUserId(userId);
        this.baseMapper.insert(shipping);
        Map<String,Integer> map = new HashMap<>();
        map.put("shippingId",shipping.getId());
        return R.ok(map);
    }

    @Override
    public R delete(Integer userId, Integer shippingId) {
        this.baseMapper.delete(new LambdaQueryWrapper<Shipping>().eq(Shipping::getId,shippingId).eq(Shipping::getUserId,userId));
        return R.ok();
    }

    @Override
    public R update(Integer userId, Integer shippingId, ShippingForm shippingForm) {
        Shipping shipping = this.baseMapper.selectById(shippingId);
        BeanUtils.copyProperties(shippingForm,shipping);
        shipping.setUserId(userId);
        this.baseMapper.updateById(shipping);
        return R.ok();
    }

}